package srw;

import java.util.HashMap;

/**
 * tree node. 
 * presents lexical or other properties of words
 *
 */
public class Property {

	/**
	 *  correspond String - name of property and Property - tree node
	 * 
	 */
	private static HashMap<String, Property> _propertiesMap = new HashMap<String, Property>();
	private String _name;
	private Property _parent;
	
	/**
	 * constructor
	 * @param name property name
	 */
	private Property(String name) {
		_name = name;
	}
	
	/**
	 * creates property and puts it to map
	 * @param name property name
	 * @return created property
	 */
	public static Property createNew(String name) {
		Property property = new Property(name);
		_propertiesMap.put(name, property);
		return property;
	}
	
	/**
	 * clear properties map
	 */
	public static void clear() {
		_propertiesMap.clear();
	}
	
	/**
	 * get propperty from map by name
	 * @param name property name
	 * @return Property
	 */
	public static Property getByName(String name) {
		return _propertiesMap.get(name);
	}

	public String getName() {
		return _name;
	}
	
	public void setParent(Property parent) {
		System.err.println(_name + " set parent " + (parent == null ? "null" : parent._name));
		_parent = parent;
	}
	
	public Property getParent() {
		return _parent;
	}
	
	/**
	 * check if given property is parent of current property
	 * @param property
	 * @return true if given property is parent of current property
	 */
	public boolean isDerived(Property property) {
		for (Property p = this; p != null; p = p._parent) {
			if (p == property)
				return true;
		}
		return false;
	}
	
}
